import GameConfig from "./GameConfig";

cc.Class({
    extends: cc.Component,

    properties: {
        // 星星
        star: cc.Prefab,
        // 初始y坐标
        initY: 0,
        // 停止移动坐标
        minY: 0,
    },

    onLoad () {
        // 星星
        this._settleStars();
        // 背景移动速度
        this.moveSpeed = 0.03;
        // 最大移动距离
        this.maxMove = 590;
    },

    start() {
        this.posY = this.initY;
    },
    
    _settleStars() {
        var starPositions = [
            {x: -251, y: 1126, scaleRate: 0.375},
            {x: -173, y: 1221},
            {x: -127, y: 1115, scaleRate: 0.75},
            {x: -192, y: 961, scaleRate: 0.75},
            {x: 319, y: 1166},
            {x: 16, y: 957, scaleRate: 0.85},
            {x: 120, y: 1344},
            {x: -309, y: 1344, scaleRate: 0.58},
            {x: 13, y: 1234},
        ];
        
        for(var i = 0; i < starPositions.length; i++) {
            var newStar = cc.instantiate(this.star);
            this.node.addChild(newStar);
            newStar.setPosition(cc.v2(starPositions[i].x, starPositions[i].y));
            if (starPositions[i].scaleRate) {
                newStar.scaleX = starPositions[i].scaleRate;
                newStar.scaleY = starPositions[i].scaleRate;
            }
        }
    },

    _move(posX, posY) {
        this.node.setPosition(cc.v2(posX, posY));
    },

    update (dt) {
        if (GameConfig.GAME_START) {
            this.posY -= this.moveSpeed;
            if (this.posY >= this.minY) {
                this._move(this.posX, this.posY);
            }
        }
    },
});
